<template>
  <j-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :maskClosable="false"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @close="close"
    cancelText="关闭"
  >
    <div style="margin: 0 auto" :class="{ isCheck: isCheck }">
      <a-form-model ref="form" :model="model" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="疑点名称" prop="ydmc">
              <a-input v-model="model.ydmc" :disabled="disableSubmit" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="疑点状态" prop="ydzt">
              <j-dict-select-tag
                style="width: 100%"
                v-model="model.ydzt"
                dict-code="generate_type"
                :disabled="disableSubmit"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="被审计单位">
              <a-input v-model="model.bsjdwName" :disabled="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="提交日期">
              <j-date style="width: 100%" v-model="model.tjrq" :disabled="disableSubmit"></j-date>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="疑点编码">
              {{ model.ydbm ? model.ydbm : '系统生成' }}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计事项" prop="sjsx">
<!--              <a-tree-select
                :replaceFields="{ children: 'children', title: 'itemName', key: 'id', value: 'id' }"
                treeDefaultExpandAll
                v-model="model.sjsx"
                style="width: 100%"
                :tree-data="treeData"
                :disabled="disableSubmit"
              />-->
              <a-input v-model="model.sjsx" :disabled="disableSubmit" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="疑点来源" prop="ydly">
              <a-input v-model="model.ydly" :disabled="disableSubmit" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-form-model-item label="疑点内容" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a-textarea allow-clear v-model="model.ydnr" :disabled="disableSubmit" />
          </a-form-model-item>
        </a-row>
<!--        <a-row>
          <a-form-model-item label="法规制度" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }" prop="fgzd">
            <a-select
              v-model="model.fgzd"
              mode="tags"
              style="width: 100%"
              option-label-prop="label"
              :disabled="disableSubmit"
            >
              <template v-for="(item, index) in regulation">
                <a-select-option :value="item.title || ''" :label="item.title" :key="id">
                  {{ item.title }}
                </a-select-option>
              </template>
            </a-select>
          </a-form-model-item>
        </a-row>-->
        <a-row v-if="!addFlag">
          <a-form-model-item label="疑点现场" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a style="color: #ef6767; border-bottom: 1px solid #ef6767" @click="toLink(model.routerData)"
              ><j-ellipsis :value="model.routerData" :length="85"
            /></a>
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="附件上传" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <RuleUpload
              :isShow="disableSubmit"
              ref="fileUpload"
              :fileid="model.fileId"
              @uploadFile="uploadFile"
            ></RuleUpload>
          </a-form-model-item>
        </a-row>
        <a-row v-if="!addFlag">
          <a-form-model-item label="现场快照" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <img-upload
              ref="imgUpload"
              :saveFile="saveFile"
              :fileData="images"
              :disabled="disableSubmit"
              @returnFile="
                (imgList) => {
                  this.model.images = imgList
                }
              "
            ></img-upload>
          </a-form-model-item>
        </a-row>
      </a-form-model>
    </div>
    <!-- table区域-end -->
  </j-modal>
</template>
<script>
import { httpAction, getAction } from '@/api/manage'
import RuleUpload from '../../../../components/RuleUpload'
import imgUpload from '@/views/sj/components/imgUpload'
import { currentYMD } from '@/mixins/commontime'

export default {
  name: 'SysPositionModal',
  props: ['id', 'bsjdwName', 'bsjdw', 'type'],
  components: {
    RuleUpload,
    imgUpload,
  },
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      disableSubmit: false,
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      regulation: [],
      treeData: [],
      saveFile: 'sys/common/uploadMultiBase64',
      dataURL: '',
      rules: {
        ydmc: [{ required: true, message: '不能为空!' }],
        ydzt: [{ required: true, message: '不能为空!' }],
        sjsx: [{ required: true, message: '不能为空!' }],
        // fgzd: [{ required: true, message: '不能为空!' }],
      },
      addFlag: false,
      images: [],
      url: {
        regulation: '/sjzszhly/knowledgeRegulation/queryKnowledgeRegulation',
        add: '/sjxmzygl/sjWorkZtfaEveYidian/add',
        edit: '/sjxmzygl/sjWorkZtfaEveYidian/edit',
        tree: '/sjxmzygl/sjWorkZtfaEve/getMyWorkZtfaEveList',
      },
      defaultYdzt: '2',
    }
  },
  created() {
    this.getRegulation()
    // this.loadTree()
  },
  watch: {},
  methods: {
    toLink(url) {
      this.$router.push(url)
    },
    loadTree() {
      debugger
      this.treeData = []
      getAction(this.url.tree, { proId: this.id, typeId: this.type }).then((res) => {
        if (res.success) {
          this.treeData = res.result
        }
      })
    },
    add() {
      this.addFlag = true
      this.model = {
        bsjdwName: this.bsjdwName,
        bsjdw: this.bsjdw,
        ydzt: this.defaultYdzt,
        tjrq: currentYMD(),
        ydly: '手工输入'
      }
      this.visible = true
    },
    check(record) {
      this.model = Object.assign(
        {
          bsjdwName: this.bsjdwName,
          bsjdw: this.bsjdw,
        },
        record
      )
      if (this.model.fgzd) {
        this.model.fgzd = this.model.fgzd.split(',')
      } else {
        this.model.fgzd = []
      }
      this.visible = true
    },
    edit(record) {
      this.model = this.$options.data().model
      if (record.images) {
        this.images = record.images
      }
      this.model = Object.assign(
        {
          bsjdwName: this.bsjdwName,
          bsjdw: this.bsjdw,
        },
        record
      )
      if (this.model.routerData) {
        this.addFlag = false
      } else {
        this.addFlag = true
      }
      if (this.model.fgzd) {
        this.model.fgzd = this.model.fgzd.split(',')
      } else {
        this.model.fgzd = []
      }
      this.visible = true
    },
    getRegulation() {
      getAction(this.url.regulation).then((res) => {
        if (res.success && res.result.length > 0) {
          this.regulation = res.result
        } else {
          console.log(res)
        }
      })
    },
    close(isSubmit) {
      if (isSubmit) {
        this.saveData()
      } else {
        this.visible = false
        this.addFlag = true
      }
    },
    saveData() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$refs.fileUpload.saveFile('sjyd')
        } else {
          return false
        }
      })
    },
    uploadFile(id) {
      console.log(this.model)
      this.model.fileId = id
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url, type
          if (!this.model.id) {
            url = this.url.add
            type = 'post'
          } else {
            url = this.url.edit
            type = 'put'
          }
          this.model.fgzd = this.model.fgzd?this.model.fgzd.toString():null
          httpAction(url, { proId: this.id, ...this.model }, type).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok')
              this.visible = false
              this.addFlag = true
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
@import '../../../../css/checkForm.less';

.name-type {
  width: 110px;
  margin-left: 5px;
  border: 1px solid #e0e0e0;

  /deep/ .ant-select-selection {
    box-sizing: border-box;
    min-height: 27px !important;

    .ant-select-selection__rendered {
      line-height: 27px !important;
    }
  }
}
</style>
